package com.ruoyi.project.modules.fileInfo.controller;

import java.util.List;

import com.ruoyi.project.modules.fileInfo.domain.FileInf;
import com.ruoyi.project.modules.fileInfo.domain.FileInfDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.fileInfo.service.IFileInfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文件信息Controller
 *
 * @author smallrain
 * @date 2021-12-06
 */
@Controller
@RequestMapping("/fileInfo/fileInfo")
@Api(tags = "图片信息")
public class FileInfoController extends BaseController {
    private String prefix = "fileInfo/fileInfo";

    @Autowired
    private IFileInfoService fileInfoService;

    @RequiresPermissions("fileInfo:fileInfo:view")
    @GetMapping()
    public String fileInfo() {
        return prefix + "/fileInfo";
    }

    /**
     * 查询文件信息列表
     */
    @RequiresPermissions("fileInfo:fileInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FileInf fileInf) {
        startPage();
        List<FileInf> list = fileInfoService.selectFileInfoList(fileInf);
        return getDataTable(list);
    }

    /**
     * 导出文件信息列表
     */
    @RequiresPermissions("fileInfo:fileInfo:export")
    @Log(title = "文件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FileInf fileInf) {
        List<FileInf> list = fileInfoService.selectFileInfoList(fileInf);
        ExcelUtil<FileInf> util = new ExcelUtil<FileInf>(FileInf.class);
        return util.exportExcel(list, "文件信息数据");
    }

    /**
     * 新增文件信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存文件信息
     */
    @RequiresPermissions("fileInfo:fileInfo:add")
    @Log(title = "文件信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FileInf fileInf) {
        return toAjax(fileInfoService.insertFileInfo(fileInf));
    }

    /**
     * 修改文件信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        FileInf fileInf = fileInfoService.selectFileInfoById(id);
        mmap.put("fileInfo", fileInf);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件信息
     */
    //@RequiresPermissions("fileInfo:fileInfo:edit")
    @Log(title = "文件信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FileInf fileInf) {
        return toAjax(fileInfoService.updateFileInfo(fileInf));
    }

    /**
     * 删除文件信息
     */
    @RequiresPermissions("fileInfo:fileInfo:remove")
    @Log(title = "文件信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(fileInfoService.deleteFileInfoByIds(ids));
    }

    /**
     * 保存页面分类图片
     * eg:
     * {
     *     "objectId":1,// 页面分类id
     *     "fileName":"".//文件名称
     *     "filePath":""//文件地址
     * }
     * @author chenxiaoyu
     * @date 2022/5/8 下午2:22
     * @param fileInfDto 图片信息
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/savePageCategoryImg")
    @ResponseBody
    @ApiOperation(value = "保存页面分类图片")
    public AjaxResult savePageCategory(@RequestBody FileInfDto fileInfDto ) {
        FileInf fileInf = new FileInf();
        BeanUtils.copyProperties(fileInfDto, fileInf);
        fileInf.setObjectType("4");
        return toAjax(fileInfoService.insertFileInfo(fileInf));
    }
}
